package logic

import (
	"context"
	"rpc/model/mysql"

	"rpc/internal/svc"
	"rpc/turn"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *turn.GoodsListRequest) (*turn.GoodsListResponse, error) {
	// todo: add your logic here and delete this line

	g := mysql.Goods{}
	list, err := g.GoodsList()
	if err != nil {
		return nil, err
	}

	var List []*turn.GoodsItem
	for _, goods := range list {
		List = append(List, &turn.GoodsItem{
			GoodsName:  goods.GoodsName,
			GoodsCover: goods.GoodsCover,
			GoodsPrice: float32(goods.GoodsPrice),
			LikeCount:  int64(goods.LikeCount),
		})
	}

	return &turn.GoodsListResponse{GoodsList: List}, nil
}
